import Vue, {nextTick} from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import MangerView from "@/views/MangerView";
import store from '@/store'


Vue.use(VueRouter)


const routes = [
  {
    path: '/login',name: '登录',component: () => import(/* webpackChunkName: "about" */ '../views/Login')
  },
  {
    path: '/register',name: '注册',component: () => import(/* webpackChunkName: "about" */ '../views/Register')
  },
  /*{
    path: '/404',name: '404',component: () => import(/!* webpackChunkName: "about" *!/ '../views/404')
  },*/
  /*{
    path: '/header',
    name: 'Header',
    component: () => import(/!* webpackChunkName: "about" *!/ '../components/Header')
  },*/
  /*{
    path: '/aside',name: 'Aside',component: () => import(/!* webpackChunkName: "about" *!/ '../components/Aside')
  },*/
  /*{
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/!* webpackChunkName: "about" *!/ '../views/AboutView.vue')
  },*/
  /*{
    path: '/manger',
    name: 'MangerView',
    component: () => import('../views/MangerView')
  },*/
 /* {
    path: '/hello',
    name: 'HelloView',
    component: () => import('../views/HomeView')
  }*/

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/*导出，再登录中使用 */
export  const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus");
  if(storeMenus){
    const mangerRoute = {
      path: '/',name: 'Manger',component: () => import('../views/MangerView'),redirect: '/Home',
      children: [
        /*{ path: 'person',name: '个人信息',component: () => import('../views/Person') },*/
      ]
    }
    const menus =JSON.parse(storeMenus)
    menus.forEach(item => {
      //若路径不为空
      if(item.menuPath){
        let itemMenu = {path: item.menuPath.replace("/",""),name: item.menuName, component: ()  => import('../views/' + item.pagePath + '.vue')}
        mangerRoute.children.push(itemMenu)
      }else if(item.children.length){
        item.children.forEach(item => {
          if(item.menuPath){
            let itemMenu = {path: item.menuPath.replace("/",""),name: item.menuName, component: ()  => import('../views/' + item.pagePath + '.vue')}
            mangerRoute.children.push(itemMenu)
          }
        })
      }
    })
    //打印路由信息
    /*console.log(router.getRoutes())*/

    const currentRouteNames =  router.getRoutes().map(v => v.name)
    if(!currentRouteNames.includes('Manger') ){
      //动态添加到现在的路由子对象中
      router.addRoute(mangerRoute)
    }

  }
}
setRoutes()
/*
router.addRoute(
{
        path: '/',
        name: 'Manger',
        component: () => import('../views/MangerView'),
        redirect: '/Home',
        children: [
          /!*设置路由属性？？？ meta()*!/
          {path: 'home',name: '主页',component: () => import(/!* webpackChunkName: "about" *!/ '../views/Home')},
          {path: 'user',name: '用户管理',component: () => import(/!* webpackChunkName: "about" *!/ '../views/User')},
          {path: 'role',name: '角色管理',component: () => import(/!* webpackChunkName: "about" *!/ '../views/Role')},
          {path: 'person',name: '个人中心',component: () => import(/!* webpackChunkName: "about" *!/ '../views/Person')},
          {path: 'file', name: '文件管理', component: () => import('../views/Files')},
          {path: 'menu', name: '模块管理', component: () => import('../views/Menu')},
          {path: 'dict', name: '字典管理', component: () => import('../views/Dict')}
        ]}
)
*/

// 路由守卫
router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称，为了在Header组件中去使用
  store.commit("setPath")  // 触发store的数据更新
/*  if(!to.matched.length){
    const storeMenus = localStorage.getItem("menus")
    if(storeMenus){
      next("/404")
    }else {
      next("/login")
    }
  }*/
  next()  // 放行路由
})



export default router
